﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLZJFurnitureSCM.BLL;
using QLZJFurnitureSCM.Model;

namespace QLZJFurnitureSCM.WinForm
{
    public partial class FrmProductPriceChangeRecord : Form
    {
        private ProductPriceChangeService ppcService = null;
        private List<ProductPriceChange> lstPpc = null;
        private List<ProductPriceChange> lstPpcShow = null;

        public FrmProductPriceChangeRecord()
        {
            InitializeComponent();
            ppcService = new ProductPriceChangeService();
            lstPpc = ppcService.FindAll();
        }

        private void FrmProductPriceChangeRecord_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'furnitureDataSet.ProductPriceChange' table. You can move, or remove it, as needed.
            //this.productPriceChangeTableAdapter.Fill(this.furnitureDataSet.ProductPriceChange);
            this.dateTimePickerSearch.Value = DateTime.Now;
            this.cboSearchType.SelectedIndex = 0;
            foreach (ProductPriceChange ppc in lstPpc)
            {
                dataGridViewPriceChangeRecord.Rows.Add(new object[] { 
                       ppc.ProductName
                      ,ppc.ProductPrice
                      ,ppc.ProductPriceNow
                      ,ppc.ProductChangeDate
                      ,ppc.ProductChangeBy
                });
            }
        }

        private void dataGridViewPriceChangeRecord_Paint(object sender, PaintEventArgs e)
        {
            if (this.dataGridViewPriceChangeRecord.Rows.Count != 0)
            {
                for (int i = 0; i < this.dataGridViewPriceChangeRecord.Rows.Count; i += 2)
                    this.dataGridViewPriceChangeRecord.Rows[i].DefaultCellStyle.BackColor = SystemColors.ActiveBorder;//System.Drawing.Color.BurlyWood;
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string keyword = this.txtKeyword.Text;
            switch(cboSearchType.SelectedIndex)
            {
                case 0:
                    lstPpcShow = lstPpc;
                    break;
                case 1:
                    if (string.IsNullOrEmpty(keyword.Trim()))
                        lstPpcShow = lstPpc;
                    else
                        lstPpcShow = (from change in lstPpc where change.ProductName.Contains(keyword) select change).ToList();
                    break;
                case 2:
                    string date = this.dateTimePickerSearch.Value.ToString("yyyyMMdd");
                        lstPpcShow = (from d in lstPpc where d.ProductChangeDate.ToString("yyyyMMdd").Contains(date) select d).ToList();
                    break;
            }
            dataGridViewPriceChangeRecord.Rows.Clear();
            foreach (ProductPriceChange ppc in lstPpcShow)
            {
                dataGridViewPriceChangeRecord.Rows.Add(new object[] { 
                       ppc.ProductName
                      ,ppc.ProductPrice
                      ,ppc.ProductPriceNow
                      ,ppc.ProductChangeDate
                      ,ppc.ProductChangeBy
                });
            }
            /*
            this.changeIdDataGridViewTextBoxColumn.DataPropertyName = "changeId";
            this.changeIdDataGridViewTextBoxColumn.HeaderText = "changeId";
            this.changeIdDataGridViewTextBoxColumn.Name = "changeIdDataGridViewTextBoxColumn";
            this.changeIdDataGridViewTextBoxColumn.ReadOnly = true;
            this.changeIdDataGridViewTextBoxColumn.Visible = false;
            // 
            // productIdDataGridViewTextBoxColumn
            // 
            this.productIdDataGridViewTextBoxColumn.DataPropertyName = "productId";
            this.productIdDataGridViewTextBoxColumn.HeaderText = "productId";
            this.productIdDataGridViewTextBoxColumn.Name = "productIdDataGridViewTextBoxColumn";
            this.productIdDataGridViewTextBoxColumn.ReadOnly = true;
            this.productIdDataGridViewTextBoxColumn.Visible = false;
            // 
            // producNameDataGridViewTextBoxColumn
            // 
            this.producNameDataGridViewTextBoxColumn.DataPropertyName = "producName";
            this.producNameDataGridViewTextBoxColumn.HeaderText = "商品名称";
            this.producNameDataGridViewTextBoxColumn.Name = "producNameDataGridViewTextBoxColumn";
            this.producNameDataGridViewTextBoxColumn.ReadOnly = true;
            // 
            // productPriceDataGridViewTextBoxColumn
            // 
            this.productPriceDataGridViewTextBoxColumn.DataPropertyName = "productPrice";
            dataGridViewCellStyle7.Format = "C1";
            dataGridViewCellStyle7.NullValue = "0";
            this.productPriceDataGridViewTextBoxColumn.DefaultCellStyle = dataGridViewCellStyle7;
            this.productPriceDataGridViewTextBoxColumn.HeaderText = "价格";
            this.productPriceDataGridViewTextBoxColumn.Name = "productPriceDataGridViewTextBoxColumn";
            this.productPriceDataGridViewTextBoxColumn.ReadOnly = true;
            // 
            // productPriceNowDataGridViewTextBoxColumn
            // 
            this.productPriceNowDataGridViewTextBoxColumn.DataPropertyName = "productPriceNow";
            dataGridViewCellStyle8.Format = "C1";
            dataGridViewCellStyle8.NullValue = "0";
            this.productPriceNowDataGridViewTextBoxColumn.DefaultCellStyle = dataGridViewCellStyle8;
            this.productPriceNowDataGridViewTextBoxColumn.HeaderText = "当前价格";
            this.productPriceNowDataGridViewTextBoxColumn.Name = "productPriceNowDataGridViewTextBoxColumn";
            this.productPriceNowDataGridViewTextBoxColumn.ReadOnly = true;
            // 
            // productChangeDateDataGridViewTextBoxColumn
            // 
            this.productChangeDateDataGridViewTextBoxColumn.DataPropertyName = "productChangeDate";
            this.productChangeDateDataGridViewTextBoxColumn.HeaderText = "修改日期";
            this.productChangeDateDataGridViewTextBoxColumn.Name = "productChangeDateDataGridViewTextBoxColumn";
            this.productChangeDateDataGridViewTextBoxColumn.ReadOnly = true;
            // 
            // productChangeByDataGridViewTextBoxColumn
            // 
            this.productChangeByDataGridViewTextBoxColumn.DataPropertyName = "productChangeBy";
            this.productChangeByDataGridViewTextBoxColumn.HeaderText = "操作人";
            this.productChangeByDataGridViewTextBoxColumn.Name = "productChangeByDataGridViewTextBoxColumn";
            this.productChangeByDataGridViewTextBoxColumn.ReadOnly = true;
            */
        }

        private void cboSearchType_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (cboSearchType.SelectedIndex)
            {
                case 0:
                    this.txtKeyword.Enabled = false;
                    this.dateTimePickerSearch.Enabled = false;
                    break;
                case 1:
                    this.txtKeyword.Enabled = true;
                    this.dateTimePickerSearch.Enabled = false;
                    break;
                case 2:
                    this.txtKeyword.Enabled = false;
                    this.dateTimePickerSearch.Enabled = true;
                    break;
            }
        }
    }
}
